<?php

$root = $_SERVER ["DOCUMENT_ROOT"];
$root .= "/registro_elettronico/";
require_once $root . 'pages.php';
require_once INC_DEFINES;
require_once INC_FUNCTIONS;
require_once MDL_DATABASE;

authorize();

$page = "";
$msg = "";
if (SAFE_REDIRECTION) {
    $data = $_SESSION;
} else {
    $data = $_GET;
}

if (check($data, array('cmd', 'table'))) {
    $table = $data['table'];
    switch ($data['cmd']) {
        case "delete":
            if (check($data, "ID")) {
                $result = delete($table, "ID=" . $data['ID']);
                if ($result) {
                    $page = HTTP_VW_NOTIFY;
                    $msg = "message=" . MSG_SUCCESSFUL_DELETE;
                } else {
                    $page = HTTP_VW_NOTIFY;
                    $msg = "message=" . MSG_FAILED_DELETE;
                }
            } else {
                $page = HTTP_VW_NOTIFY;
                $msg = "message=ID not set!";
            }
            break;
        case "insert":
            $values = array ();
            foreach ($data as $key => $value) {
                switch ($key) {
                    case "cmd":
                    case "table":
                        break;
                    default :
                        $values[$key] = $value;
                }
            }
            if (insert($data['table'], $values)) {
                $page = HTTP_VW_NOTIFY;
                $msg = "message=" . MSG_SUCCESSFUL_INSERT;
            } else {
                $page = HTTP_VW_NOTIFY;
                $msg = "message=" . MSG_FAILED_INSERT;
            }
            break;
        case "select":
            $fields = null;
            $where = null;
            $group_by = null;
            $order_by = null;
            $limit = null;
            $distinct = null;
            foreach ($data as $key => $value) {
                switch ($key) {
                    case "cmd":
                    case "table":
                        break;
                    case "where":
                        $where = $value;
                        break;
                    case "group_by":
                        $group_by = $value;
                        break;
                    case "order_by":
                        $order_by = $value;
                        break;
                    case "limit":
                        $limit = $value;
                        break;
                    case "distinct":
                        $distinct = $value;
                        break;
                    default:
                        $fields[$key] = $value;
                }
            }
            if (select($table, $fields, $where, $group_by, $order_by, $limit, $distinct)) {
                $page = HTTP_VW_NOTIFY;
                $msg = "message=" . MSG_SUCCESSFUL_SELECT;
            } else {
                $page = HTTP_VW_NOTIFY;
                $msg = "message=" . MSG_FAILED_SELECT;
            }
            break;
        case "update":
            if (check($data, "ID")) {
                foreach ($data as $key => $value) {
                    switch ($key) {
                        case "cmd":
                        case "ID":
                        case "table":
                            break;
                        default :
                            $values[$key] = $value;
                            break;
                    }
                }
                if (update($table, $values, "ID=" . $data['ID'])) {
                    $page = HTTP_VW_NOTIFY;
                    $msg = "message=" . MSG_SUCCESSFUL_UPDATE;
                } else {
                    $page = HTTP_VW_NOTIFY;
                    $msg = "message=" . MSG_FAILED_UPDATE;
                }
            } else {
                $page = HTTP_VW_NOTIFY;
                $msg = "message=ID not set!";
            }
            break;
    }
} else {
    $page = HTTP_VW_NOTIFY;
}
//usare location: $table.php?$args
if (SAFE_REDIRECTION) {
    $_SESSION['msg'] = $msg;
}
//header ("location: $page?$msg");
?>